#include <stdlib.h>
#include <math.h>

#include <util/msg.h>
#include "../../matrix/matrix.h"

static real sobel_data_3x3_Dx[9] = {1, 0, -1, 2, 0, -2, 1, 0, -1};
static real sobel_data_3x3_Dy[9] = {1, 2, 1, 0, 0, 0, -1, -2, -1};

static real sobel_data_3x1_Dx[3] = {1, 2, 1};
static real sobel_data_1x3_Dx[3] = {1, 0, -1};

static real sobel_data_3x1_Dy[3] = {1, 0, -1};
static real sobel_data_1x3_Dy[3] = {1, 2, 1};

ImpMatrix imp_filter_sobel(int dim, char or, int comp) {
	ImpMatrix filt;

	if( dim != 3 ) {
		msg_error("Dimension %d not avaliable.\n", dim);
	}
	if( or != 'x' && or != 'y' ) {
		msg_error("Orientation '%c' not avaliable.\n", or);
	}
	if( comp < 0 || comp > 2 ) {
		msg_error("Component %d must be:\n"
				"\t0: for full matrix.\n"
				"\t1: for left nx1 vector.\n"
				"\t2: for right 1xn vector", dim);
	}
	
	// <editor-fold defaultstate="collapsed" desc="3x3 filter components">
	if (dim == 3 && comp == 0) {
		filt.rows = dim;
		filt.columns = dim;
		filt.size = dim*dim;
		if (or == 'x') filt.data = sobel_data_3x3_Dx;
		if (or == 'y') filt.data = sobel_data_3x3_Dy;
	}
	if (dim == 3 && comp == 1) {
		filt.rows = dim;
		filt.columns = 1;
		filt.size = dim;
		if (or == 'x') filt.data = sobel_data_3x1_Dx;
		if (or == 'y') filt.data = sobel_data_3x1_Dy;
	}
	if (dim == 3 && comp == 2) {
		filt.rows = 1;
		filt.columns = dim;
		filt.size = dim;
		if (or == 'x') filt.data = sobel_data_1x3_Dx;
		if (or == 'y') filt.data = sobel_data_1x3_Dy;
	}// </editor-fold>

	// =========================================================================
	
	return filt;
}
